<?php
/**
 * @copyright meila.com
 * @author shaopu@
 * @name 检测昵称是否可用
 * @param
 *         $nick = 作用
 *
 *
 */

ini_set('display_errors', 1);
error_reporting(E_ALL);
include('../__global.php');



include(SERVER_ROOT_PATH.'/include/config/dataRule/ml_datarule_content.php');
include(SERVER_ROOT_PATH.'/include/config/ml_queue_name.php');



class aj_publish extends ml_controller
{
    private $content;
    private $pic_id;
    private $gd_title;
    private $gd_price;
    private $gd_url;
    private $gd_pic;
    private $type_xrank;
    private $pb2weibo;
    private $ecode;
    private $arrAlert = array(
        'content' => 'content',
        'content_len' => 'content_len',
        'gd_title_len' => 'gd_title_len',
        'gd_price' => 'gd_price',
        'gd_url' => 'gd_url',
        'gd_pic' => 'gd_pic',
        'gd_exit' => 'gd_exit',
    );

    private $infoAndName;
    private $col_info;
    private $col_id;
    private $uid;

    function initParam()
    {
        /**
         * @todo get post
         */
        $this->content = $this->input('content', 'P', '');
        $this->pic_id =  $this->input('pic_id', 'P', 0);
        $this->gd_title = $this->input('gd_title', 'P', '');
        $this->gd_price = $this->input('gd_price', 'P', 0);
        $this->gd_url = $this->input('gd_url', 'P', '');
        $this->gd_pic = $this->input('gd_pic', 'P', '');
        $this->pb2weibo = $this->input('pb2weibo','P');
        $this->col_id = intval($this->input('col_id','P'));
        $this->ecode = $this->input('ecode', 'P');
        $this->uid = $this->input('uid','P');

    }
    /**
     * @develop_template
     * 检查参数合法性
     */
    function checkParam()
    {



        /**
         * @todo 待加敏感词过滤
         */
        //$this->content = ml_tool_keyword::filterSensitiveWord($this->content);
        //if(empty($this->ecode)) exit;

        if(empty($this->content)){
            $this->api_output(ML_RCODE_PARAM , '', $this->arrAlert['content']);
        }

        if(Tool_string::str_width($this->content) > ML_DATARULE_CONTENT_MAXLEN)
        $this->api_output(ML_RCODE_PARAM , '', $this->arrAlert['content_len']);

        if(!empty($this->gd_title) && !empty($this->gd_url)){
            if(Tool_string::str_width($this->gd_title) > ML_DATARULE_CONTENT_TITLE_MAXLEN){
                $this->api_output(ML_RCODE_PARAM , '', $this->arrAlert['gd_title_len']);
            }
            if(!empty($this->gd_price)&&!is_numeric($this->gd_price)){
                $this->api_output(ML_RCODE_PARAM , '', $this->arrAlert['gd_price']);
            }

            if(!empty($this->gd_url) && !preg_match(ML_DATARULE_BASE_URL , $this->gd_url))
            $this->api_output(ML_RCODE_PARAM ,'', $this->arrAlert['gd_url']);
            if(!empty($this->gd_pic) && !preg_match(ML_DATARULE_BASE_URL , $this->gd_pic))
            $this->api_output(ML_RCODE_PARAM ,'', $this->arrAlert['gd_pic']);



            if(empty($this->pic_id)){
                $this->type_xrank = ML_CONTENT_XRANK_GOODS;


                $fetchImg = 'http://'.ML_CNF_DOMAIN.'/image/fetch_pic_to_goods.php';
                $data['uid'] = $this->uid;
                $data['url'] = $this->gd_pic;
                $rs = Tool_http::post($fetchImg, $data);
                
                $rs = json_decode($rs, true);



                if ($rs['code'] == ML_RCODE_SUCC) {
                    $this->pic_id = $rs['data']['pid'];
                } else {

                    $this->api_output(ML_RCODE_PARAM ,'', $this->arrAlert['pic_id']);
                }


            }else{

                $this->type_xrank = ML_CONTENT_XRANK_IMGOODS;
            }
        }else{
            if(!empty($this->gd_price) || !empty($this->gd_url) || !empty($this->gd_pic)){
                $this->api_output(ML_RCODE_PARAM ,'', $this->arrAlert['gd_exit']);
            }

            if(!empty($this->pic_id)){
                $this->type_xrank = ML_CONTENT_XRANK_IMG;
            }
        }


    }
    /**
     * @develop_template
     * 核心业务逻辑
     */
    function main()
    {


        $oGenerator = new ml_model_dbIdGenerator();
        $time = time();
        $uid = $this->uid;

        $aNicks = ml_tool_nick::findAllAtNick($this->content);
        if($aNicks)
        {
            $aN2u = ml_tool_nick::nicks2uids($aNicks);
        }

        /**
         * 有图片的处理方式
         */
        if(!empty($this->pic_id)){

            $content_res = $oGenerator->get_content_rid($uid, $content_rid);
            $userfeed_res = $oGenerator->get_userfeed_rid($uid, $userfeed_rid);
            if(!$content_res && $userfeed_res){
                $this->api_output(ML_RCODE_BUSY , '' , ML_RMESSAGE_BUSY);
            }
            $content_data['type_xrank'] = $this->type_xrank;
            $userfeed_data['content_rid'] = $content_data['rid'] = $content_rid;
            $userfeed_data['uid'] = $content_data['uid'] = $uid;
            $userfeed_data['content'] = $content_data['content'] = $this->content;
            $userfeed_data['pic_id'] = $content_data['pic_id'] = $this->pic_id;
            $userfeed_data['rid'] = $content_data['userfeed_rid'] = $userfeed_rid;
            $userfeed_data['ctime'] = $content_data['ctime'] = date('Y-m-d H:i:s', $time);

            $userfeed_data['type'] = Tool_dataMultiplex::isAttribute(ML_CONTENT_XRANK_GOODS,$this->type_xrank) ? ML_FEED_TYPE_PUB_DOTEY : ML_FEED_TYPE_PUB_PIC;
            $userfeed_data['cip'] = $content_data['cip'] = Tool_ip::get_real_ip();
            if(!empty($this->gd_title)){
                $content_data['gd_title'] = $this->gd_title;
                $content_data['gd_price'] = $this->gd_price;
                $content_data['gd_info'] = array(
                    'gd_url'=>$this->gd_url,
                    'gd_pic'=>$this->gd_pic,
                );
                    
                $userfeed_data['feed_info'] = array(
                    'gd_title'=>$this->gd_title,
                    'gd_price'=>$this->gd_price,
                    'gd_url'=>$this->gd_url,
                    'gd_pic'=>$this->gd_pic,
                );

                //将用户输入的昵称保存备查 shaopu@
                if(!empty($aN2u))
                {
                    $content_data['gd_info']['nick2uid']= $aN2u;
                    $userfeed_data['feed_info']['nick2uid']= $aN2u;
                }
            }
            $oContent = new ml_model_dbContent();

            $rDb_content = $oContent->insert_content($content_data);
            if($rDb_content){
                if(!empty($this->gd_title))
                $rMq_analyse = ml_tool_sendMq::goods_title_analyse($content_rid, $this->ecode);
                //$rMq_content = ml_tool_sendMq::goods_add_content($content_data);
                    
                ml_tool_sendMq::search_content('A',$content_rid);

                if(!empty($this->col_id)&&$this->col_id>0){//收集到专辑
                    $modelRe=new ml_model_dbCollectionRe();
                    $modelBox=new ml_model_dbCollectionBox();
                    $comModel=new ml_model_dbCollectionComment();
                    $this->get_col_id($modelBox);//其中的name用来插入评论表，作为冗余name
                    $this->col_info_factory();
                    $modelRe->submitRepost($this->uid,$this->col_id,$content_rid,'');//向关系表中加一个记录
                    $modelBox->submitRepost($this->uid,$this->col_id,$this->col_info);//采集数+1&update专辑的前9个图片
                    $comModel->insert_comment($content_rid, $this->infoAndName['name'], $this->uid, $this->col_id, '');    
                    $redis=new ml_model_rdsCount();
                    $redis->incrColCnt($content_rid);//宝贝被转采数+1
                }
                    
            }
        }else{

            $userfeed_res = $oGenerator->get_userfeed_rid($uid, $userfeed_rid);
            if(!$userfeed_res){
                $this->api_output(ML_RCODE_BUSY , '' , ML_RMESSAGE_BUSY);
            }
            $userfeed_data['rid'] = $userfeed_rid;
            $userfeed_data['uid'] = $uid;
            $userfeed_data['content'] = $this->content;
            $userfeed_data['ctime'] = date('Y-m-d H:i:s', $time);
            $userfeed_data['type'] = ML_FEED_TYPE_PUB_SHORT;
            //将用户输入的昵称保存备查 shaopu@
            if(!empty($aN2u))
            {
                $userfeed_data['feed_info']['nick2uid'] = $aN2u;
            }
        }

        $rid = $content_rid ? $content_rid : $userfeed_rid;

        if(!empty($aN2u))
        ml_tool_sendMq::atme_in_content($uid , $rid , array_values($aN2u));


        $oUserfeed = new ml_model_dbUserfeed();
        $rDb_userfeed = $oUserfeed->insert_content($userfeed_data);



        /**
         * 增加内容数统计
         */
        $oRedis = new ml_model_rdsCount();
        $oRedis->incrUserContentcnt($uid);
        $oRedis->incrUserfeedcnt($uid);

        /**
         * @todo 发布到微博待处理
         */

        if(!$rDb_userfeed){
            $this->api_output(ML_RCODE_BUSY);
        }else{
            $data = array('feed_rid' => $userfeed_rid, 'uid' => $uid);
            ml_tool_sendMq::add_feed($data);

            $this->api_output(ML_RCODE_SUCC,array('uid'=>$uid,'rid'=>$content_rid));
        }
    }
    private function get_col_id(&$model){
        $this->infoAndName=$model->getColinfo($this->uid, $this->col_id);
        if (empty($this->infoAndName['name'])) {
            $this->api_output(ML_RCODE_PARAM,'','wrong col_id');
        }

    }
    private function col_info_factory(){
        $this->col_info=$this->infoAndName['col_info'];
        $this->col_info=empty($this->col_info)?array():$this->col_info;
        if(count($this->col_info)>=9) array_pop($this->col_info);
        $this->col_info=array_merge(array($this->pic_id),$this->col_info);

    }
}


new aj_publish();

